﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GeneralServices.Data
{
    /// <summary>
    /// 
    /// </summary>
    public static class UnitOfWorkGeneral
    {
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        public static void RollBackTotal<T>(String key)
            where T : class
        {
            DbContext context = StoreApplicationDbContext.Get(key) as DbContext;
            if (context != null)
            {
                foreach (DbEntityEntry entry in context.ChangeTracker.Entries<T>())
                {
                    object o = entry.Entity;
                    if (entry.State == System.Data.EntityState.Modified ||
                         entry.State == System.Data.EntityState.Added ||
                        entry.State == System.Data.EntityState.Deleted ||
                        entry.State == System.Data.EntityState.Unchanged ||
                        entry.State == System.Data.EntityState.Added)
                        entry.State = System.Data.EntityState.Detached;
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        public static void RollBackTotal<T>(DbContext context)
        where T : class
        {
            if (context != null)
            {
                foreach (DbEntityEntry entry in context.ChangeTracker.Entries<T>())
                {
                    object o = entry.Entity;
                    if (entry.State == System.Data.EntityState.Modified ||
                         entry.State == System.Data.EntityState.Added ||
                        entry.State == System.Data.EntityState.Deleted ||
                        entry.State == System.Data.EntityState.Unchanged ||
                        entry.State == System.Data.EntityState.Added)
                        entry.State = System.Data.EntityState.Detached;
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="context"></param>
        public static void SaveT<T>(DbContext context)
        where T : class
        {
            if (context != null)
            {
                foreach (DbEntityEntry entry in context.ChangeTracker.Entries())
                {
                    if (((T)entry.Entity) == null &&
                        (entry.State == System.Data.EntityState.Modified ||
                         entry.State == System.Data.EntityState.Added ||
                        entry.State == System.Data.EntityState.Deleted ||
                        entry.State == System.Data.EntityState.Unchanged ||
                        entry.State == System.Data.EntityState.Added))
                        entry.State = System.Data.EntityState.Detached;

                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        public static void SaveT<T>(String key)
        where T : class
        {
            DbContext context = StoreApplicationDbContext.Get(key) as DbContext;

            if (context != null)
            {
                foreach (DbEntityEntry entry in context.ChangeTracker.Entries())
                {
                    if (((T)entry.Entity) == null &&
                        (entry.State == System.Data.EntityState.Modified ||
                         entry.State == System.Data.EntityState.Added ||
                        entry.State == System.Data.EntityState.Deleted ||
                        entry.State == System.Data.EntityState.Unchanged ||
                        entry.State == System.Data.EntityState.Added))
                        entry.State = System.Data.EntityState.Detached;

                }
            }
        }

    }
}
